//
//  TKYChangePwdController.m
//  StationHouseHelper
//
//  Created by apple on 2018/6/15.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYChangePwdController.h"

@interface TKYChangePwdController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *oldPwdTF;
@property (weak, nonatomic) IBOutlet UITextField *currtentPwdTF;
@property (weak, nonatomic) IBOutlet UITextField *turePwdTF;

@end

@implementation TKYChangePwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置界面
    [self setupUI];
}


//设置界面
- (void)setupUI{
    self.navigationItem.title = @"修改密码";
    self.view.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1/1.0];
    self.navigationController.navigationBar.barTintColor = KNavColor;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)];
    
    self.oldPwdTF.delegate = self;
    self.currtentPwdTF.delegate = self;
    self.turePwdTF.delegate = self;
}

#pragma mark-- 保存按钮的点击
- (void)saveAction:(UIBarButtonItem *)barButtonItem{
   
    if (self.oldPwdTF.text.length == 0) {
        [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:14 status:@"旧密码不能为空"];
        return;
    }else{
        if(![self checkPassword:self.oldPwdTF.text]){
            [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:14 status:@"旧密码不能符合"];
            return;
        }
    }
    
    if (self.currtentPwdTF.text.length == 0) {
        [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:14 status:@"新密码不能为空"];
        return;
    }else{
        if (![self checkPassword:self.currtentPwdTF.text]) {
            [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:14 status:@"新密码不能符合"];
            return;
        }
    }
    
    if (![self.turePwdTF.text isEqualToString:self.currtentPwdTF.text]) {
        [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:14 status:@"两次秘密不一致"];
        return;
    }
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.oldPwdTF) {
//        if([self checkPassword:self.oldPwdTF.text]){
//            [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:14 status:@"能符合"];
//            ;
//            r
//        }else{
//            [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:14 status:@"不能符合"];
//        }
        
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.oldPwdTF.text.length >= 12) {
             [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:14 status:@"最多只能输入12个字符"];
            self.oldPwdTF.text = [textField.text substringToIndex:12];
            return NO;
        }
        return YES;

    }else if (textField == self.currtentPwdTF){
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.currtentPwdTF.text.length >= 12) {
             [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:14 status:@"最多只能输入12个字符"];
            self.oldPwdTF.text = [textField.text substringToIndex:12];
            return NO;
        }
        
        return YES;

    }else{
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.turePwdTF.text.length >= 12) {
            [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:14 status:@"最多只能输入12个字符"];
            self.oldPwdTF.text = [textField.text substringToIndex:12];
            return NO;
        }
        return YES;

    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    if(textField == self.oldPwdTF){
       
    }
}


- (BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @"^(?=.*[a-zA-Z0-9].*)(?=.*[a-zA-Z\\W].*)(?=.*[0-9\\W].*).{6,12}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
